//
//  BaseScrollView.m
//  智云题库
//
//  Created by 毕武侠 on 2017/4/16.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "BaseScrollView.h"
#import "UIScrollView+EmptyDataSet.h"

@interface BaseScrollView () <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) NSString *rmodel;
@property (nonatomic, strong) BaseViewModel *viewModel;     // 网络请求
@property (nonatomic, assign) BOOL isLoadedView;
@property (nonatomic, assign) BOOL showDisplayEmpty;
@property (nonatomic, getter=isLoading) BOOL loading;

@property (nonatomic, strong) NSString *emptyTitle;
@property (nonatomic, strong) NSString *emptyImage;

@end

@implementation BaseScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initstance];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initstance];
    }
    return self;
}

// 初始化
- (void)initstance {
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
    
    self.showDisplayEmpty=YES;
    self.isLoadedView = NO;
    self.loading = NO;
}

#pragma mark - load data
- (void)setupLoadDataWithUrl:(NSString *)url params:(NSDictionary *)params model:(NSString *)model {
    self.viewModel = [[BaseViewModel alloc] initWithUrl:url];
    self.viewModel.params = [[SGParameter alloc] init];
    self.viewModel.params.body = params;
    
    self.receiveSignal = self.viewModel.command.executionSignals.switchToLatest;
    self.errorSignal = self.viewModel.command.errors;
    
    [self.viewModel.command.executing subscribeNext:^(NSNumber *executing) {
        self.loading = executing.boolValue;
    }];
    self.rmodel = model;
}

// 方式二, 请求
- (void)loadData {
    [self loadDataWithParams:nil showLoadingView:NO];
}
- (void)loadDataShowLoadingView:(BOOL)showLoadingView {
    [self loadDataWithParams:nil showLoadingView:showLoadingView];
}
- (void)loadDataWithParams:(NSDictionary *)params {
    [self loadDataWithParams:params showLoadingView:NO];
}
- (void)loadDataWithParams:(NSDictionary *)params showLoadingView:(BOOL)showLoadingView {
    self.viewModel.params.body = params;
    if (showLoadingView == YES) {
        [self setupLoadingView:self];
    }
    
    [self.viewModel.command execute:self.rmodel];
    [self reloadEmptyDataSet];
}

// 请求时，增加转圈
- (void)setupLoadingView:(UIView *)view {
    if (self.isLoadedView == YES) { // 避免多次加载
        return;
    }
    
    self.isLoadedView = YES;
    [self.viewModel.command.executionSignals.switchToLatest subscribeNext:^(id receive) {
        [MBProgressHUD hideHUDForView:view animated:YES];
        [self reloadEmptyDataSet];
    }];
    
    [self.viewModel.command.executing subscribeNext:^(NSNumber *executing) {
        if (executing.boolValue) {      // 正在加载
            [MBProgressHUD showLoadingImageToView:view];
        }
    }];
    
    [self.viewModel.command.errors subscribeNext:^(NSError *error) {
        [MBProgressHUD showTextOnly:view message:self.viewModel.msg];
    }];
}

#pragma mark - empty view
- (void)showEmptyView {
    [self showEmptyViewWithTitle:@"暂无数据" image:@"小麦"];
}
- (void)showEmptyView:(BOOL)show {
    if (show == YES) {
        [self showEmptyView];
    } else {
        [self showEmptyViewWithTitle:@"暂无数据" image:@"小麦" show:NO];
    }
}
- (void)showNotNetworkView {
    [self showEmptyViewWithTitle:@"暂无网络" image:@"小麦"];
}
- (void)showNotNetworkView:(BOOL)show {
    if (show == YES) {
        [self showEmptyViewWithTitle:@"暂无网络" image:@"小麦"];
    } else {
        [self showEmptyViewWithTitle:@"暂无网络" image:@"小麦" show:NO];
    }
}
- (void)showEmptyViewWithTitle:(NSString *)title image:(NSString *)image {
    self.emptyTitle = title;
    self.emptyImage = image;
    
    self.showDisplayEmpty = YES;
    [self reloadEmptyDataSet];
}
- (void)showEmptyViewWithTitle:(NSString *)title image:(NSString *)image show:(BOOL)show {
    self.emptyTitle = title;
    self.emptyImage = image;
    
    self.showDisplayEmpty = show;
    [self reloadEmptyDataSet];
}

#pragma mark - DZNEmptyDataSetSource Methods
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = nil;
    UIFont *font = nil;
    UIColor *textColor = nil;
    
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    
    text = self.isLoading == YES ? @"" : (self.emptyTitle ? : @"");
    font = [UIFont systemFontOfSize:14.0];
    textColor = [UIColor grayColor];
    
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *name = [self loadImageName];
    return name ? [UIImage imageNamed:name] : nil;
}

- (NSString *)loadImageName {
    return self.isLoading == YES ? @"loading_imgBlue" : self.emptyImage;
}

- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.repeatCount = MAXFLOAT;
    
    return animation;
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIColor whiteColor];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -(SCREEN_HEIGHT / 6);
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    return 0.0;
}

#pragma mark - DZNEmptyDataSetDelegate Methods

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return self.showDisplayEmpty;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

// YES: 开始转圈
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView {
    return self.isLoading;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (self.isLoading) {
        return;
    }
    
    self.loading = YES;
    [self loadData];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    if (self.isLoading) {
        return;
    }
    
    self.loading = YES;
    [self loadData];
}

@end
